package common.web.http;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**
 * 自定义json消息转换器
 * 增加自定义属性的过滤
 * @author MRJ
 *
 */
public class FastJsonHttpFilterMessageConverter extends FastJsonHttpMessageConverter {

	private Charset charset = UTF8;
	private SerializerFeature[] features = new SerializerFeature[0];
	private SerializeFilter[] filters = new SerializeFilter[0];

	@Override
	protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException,
			HttpMessageNotWritableException {

		OutputStream out = outputMessage.getBody();
		String text = JSON.toJSONString(obj, filters, features);
		byte[] bytes = text.getBytes(charset);
		out.write(bytes);
	}

	public Charset getCharset() {
		return charset;
	}

	public void setCharset(Charset charset) {
		super.setCharset(charset);
		this.charset = charset;
	}

	public SerializerFeature[] getFeatures() {
		return features;
	}

	public void setFeatures(SerializerFeature... features) {
		super.setFeatures(features);
		this.features = features;
	}

	public SerializeFilter[] getFilters() {
		return filters;
	}

	public void setFilters(SerializeFilter[] filters) {
		this.filters = filters;
	}

}
